package com.shadow.bus;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.shadow.bus.event.LoadMainEvent;
import com.shadow.bus.event.LogoutEvent;
import com.shadow.bus.event.PopChildEvent;
import com.shadow.bus.login.LoginDelegate;
import com.shadow.bus.main.MainDelegate;
import com.zhangyu.shadow.core.account.AccountManager;
import com.zhangyu.shadow.core.account.IUserChecker;
import com.zhangyu.shadow.core.delegate.bottom.BaseMainFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import busfix.tepth.com.shadow_bus.R;

/**
 * @author zhangyu
 * @date 2018/7/2
 */

public class RootDelegate extends BaseMainFragment {
    public static RootDelegate newInstance() {
        return new RootDelegate();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_root;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        EventBus.getDefault().register(this);
    }
    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        if (savedInstanceState == null) {
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSignIn() {
                    loadRootFragment(R.id.fl_first_container, MainDelegate.newInstance());
                }

                @Override
                public void onNotSignIn() {
                    loadRootFragment(R.id.fl_first_container, LoginDelegate.newInstance());
                }
            });
        }
    }

    @Subscribe
    public void onPopChild(PopChildEvent event) {
        popToChild(MainDelegate.class, false);
    }

    @Subscribe
    public void onLogout(LogoutEvent event) {
        popToChild(MainDelegate.class, false);
        popChild();
        loadRootFragment(R.id.fl_first_container, LoginDelegate.newInstance());
    }

    @Subscribe
    public void onLoadMain(LoadMainEvent event) {
        popChild();
        loadRootFragment(R.id.fl_first_container, MainDelegate.newInstance());
        //popToChild(MainContentFragment.class, false);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }
}
